// import { getQueryVariable } from '@/utils/publicUtils'
import { reportLog } from '@/utils/reportLog'
import { ResourceCardStylesCache } from '@/utils/cache-util'

// 控制权限：有权限可上报、无权限展示&禁止点击
// v-btnPermi="{permi: 'p_btn_xxx'}"
// 控制权限：有权限可上报、无权限删除节点
// v-btnPermi="{permi: 'p_btn_xxx', isHiden: true}"
// 控制权限：有权限展示&不上报数据、无权限删除节点
// v-btnPermi="{permi: 'p_btn_xxx', noReport: true, isHiden: true}"

// 控制权限：接收额外隐藏权限list，如果当前权限存在隐藏列表中，不展示
// v-btnPermi="{permi: 'p_btn_xxx', isHiden: true, hideList: []}"

export default {
  created(el, binding) {
    const { value } = binding
    // let currentRid = getQueryVariable('rid')
    if (!value.noReport) {
      // 用户有按钮权限：给按钮增加监听事件, 上报日志
      el.addEventListener('click', () => {
        // 点击按钮，上报日志
        reportLog({
          type: 4,
          resourceKey: value.permi,
          // pageKey: currentRid
        })
      })
    }
  },
  beforeMount(el, binding) {
    const { value } = binding
    let list = JSON.parse(localStorage.getItem('portalResourceList'))
    // if (process.env.NODE_ENV !== 'development') {
    let hasPermi = list && list.indexOf(value.permi) > -1 ? true : false
    if (!hasPermi) {
      el.disabled = true
      el.style = "cursor: no-drop"
    }
    if (value.isHiden && !hasPermi) {
      el.style = "display: none"
    }

    let hideList = value.hideList ? value.hideList : []
    let isHide = hideList.indexOf(value.permi) == -1 ? false : true
    if (value.isHiden && isHide) {
      el.style = "display: none"
    }
    // }
  },
  mounted(el, binding) {
    const { value } = binding
    if (ResourceCardStylesCache.get()) {
      // 1. 在缓存dashBoardResourceCardStyles里面能找到permi 2. 增加class
      // 0-正常, 1-新上线, 2-UAT
      ResourceCardStylesCache.get()[value.permi] == 1 ? el.classList.add('db-card-new') : ''
      ResourceCardStylesCache.get()[value.permi] == 2 ? el.classList.add('db-card-uat') : ''
    }
  },
  // updated(el, binding) {
  //   // 如果绑定值是真实的
  //   if (binding.value) {
  //     // 添加 class
  //     el.classList.add('my-class');
  //   } else {
  //     // 如果绑定值是假的，则移除 class
  //     el.classList.remove('my-class');
  //   }
  // }
}